<template>
	<view class="search">
		<view class="searchBox marginTop10">
			<image src="../../../static/img/each.png" mode=""></image>
			<!-- <input  confirm-type="search" v-model="updata" placeholder="输入搜索关键词" @confirm="search($event)"/> -->
			<input  confirm-type="search" v-model="updata" placeholder="输入搜索关键词" @confirm="search($event,'')"/>
		</view>
		<view class="container">
			<view class="disFlex title">
				<view class="word">历史搜索</view>
				<view class="iconfont icon-shanchu"></view>
			</view>
			<view class="history">
				<view class="box" v-for="(val,index) in dataarr" :key="index" @tap="search($event,val)">
					<text class="word">{{val}}</text>
					<image class="col" src="../../../static/img/col.png" mode="" @tap.stop="col(index)"></image>
				</view>
			</view>
		</view>
		
		<view class="host" style="margin-top: 80rpx;">
			<view class="disFlex title">
				<view class="word">热门搜索</view>
			</view>
			<view class="history">
				<view class="box" v-for="(v,i) in hot" :key="i" @tap="search($event,v)">
					<text class="word">{{v}}</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				dataarr:[],
				hot:[],
			}
		},
		onLoad() {
			this.gethistory()
			this.gethot()
		},
		methods: {
			search(e,val){
				console.log(e,val)
				if(val==''){
					if(e.detail.value!=''){
					this.dataarr.push(e.detail.value)
					this.dataarr=[...new Set(this.dataarr)]
					let data=JSON.stringify(this.dataarr)
					uni.setStorageSync('history', data );
					uni.navigateTo({
						url: '/pages/index/search/searchPackageSharing?id=' + e.detail.value
					});	
					}
				}else{
					uni.navigateTo({
						url: '/pages/index/search/searchPackageSharing?id=' + val
					});	
				}
			},
			gethistory(){
				if(uni.getStorageSync('history').length!=0){
					
				this.dataarr=JSON.parse(uni.getStorageSync('history'))
				}
				console.log(this.dataarr)
			},
			gethot(){
				this.http('/api/project/hot_search', 'post', {}, )
					.then((res) => {
						this.hot = res.data
					})
			},
			col(i){
				this.dataarr.splice(i,1)
				let data=JSON.stringify(this.dataarr)
				uni.setStorageSync('history', data );
				this.gethistory()
			}
		}
	}
</script>

<style lang="less">
	.search{
		padding: 0 40rpx;
		.searchBox{
			height:80rpx;
			background:rgba(245,245,249,1);
			border-radius:40rpx;
			display: flex;
			align-items: center;
			padding: 0 30rpx;
			image{
				width: 34rpx;
				height: 34rpx;
			}
			input{
				padding-left: 8rpx;
				width: 100%;
			}
		}
		.container,.host{
			margin-top: 50rpx;
			width: calc(100% + 20rpx);
			.title{
				.word{
					font-size: 32rpx;
				}
			}
			.history{
				display: flex;
				align-items: center;
				flex-wrap: wrap;
				.box{
					background:rgba(245,245,249,1);
					border-radius:30rpx;
					padding: 10rpx 30rpx;
					position: relative;
					margin-top: 30rpx;
					margin-right: 20rpx;
					.word{
						color: #BEC9D7;
					}
					
					.col{
						width: 34rpx;
						height: 34rpx;
						position: absolute;
						top: -10rpx;
						right: -10rpx;
					}
				}
			}
		}
		
	}
</style>
